import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'


const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

//解决重复点导航，报错的问题
// const originalReplace = VueRouter.prototype.replace
// VueRouter.prototype.replace = function replace(location) {
//    return originalReplace.call(this, location).catch(err => err)
// }



Vue.use(VueRouter)

const routes = [
    {
        path: "/",
        redirect: "/home",
    },
    {
        path: "/home",
        name: "Home",
        component: Home,
    },
    {
        path: "/list",
        name: "List",
        component: () =>
            import("../views/List.vue"),
    },
    {
        path: "/cart",
        name: "Cart",
        component: () =>
            import("../views/Cart.vue"),
    },
    {
        path: "/my",
        name: "My",
        meta: {
            keepAlive: true,  //此组件需要缓存  
        },
        component: () =>
            import("../views/My.vue"),
    },
    {
        path: "/search",
        name: "Search",
        children: [
            {
            
                path: "/",
                name: "index",
                component: () =>
                    import("../views/search/Search-index.vue"),
            },
            {
            
                path: "goods",
                name: "goods",
                component: () =>
                    import("../views/search/Search-goods.vue"),
            },
        ],
        component: () =>
            import("../views/Search.vue"),
    },
    {
        path: "/detail",
        name: "Detail",
        meta: {
            keepAlive: true,  //此组件需要缓存  
        },
        component: () =>
            import("../views/Detail.vue"),
    },
    {
        path: "/login",
        name: "Login",
        component: () =>
            import("../views/login/Login.vue"),
    },
    {
        path: "/userlogin",
        name: "UserLogin",
        component: () =>
            import("../views/login/UserLogin.vue"),
    },
    {
        path: "/register",
        name: "Register",
        component: () =>
            import("../views/login/Register.vue"),
    },
    {
        path: "/recovery",
        name: "Recovery",
        children: [
            {
            
                path: "/",
                name: "index",
                component: () =>
                    import("../views/recovery/RecoveryIndex.vue"),
            },
            {
            
                path: "btn",
                name: "btn",
                component: () =>
                    import("../views/recovery/RecoveryBtn.vue"),
            },
        ],
        component: () =>
            import("../views/recovery/Recovery.vue"),
    },

    {
        path: '/address',
        name: 'Address',
        component: () =>
            import("../views/Address.vue"),
        children: [
            {
                path: '/',
                name: 'AddressIndex',
                component: () =>
                    import("../views/address/Address-Index.vue"),
            },
            {
                path: 'addAddress',
                name: 'add-address',
                component: () =>
                    import("../views/address/Add-Address.vue"),
            },
        ],
    },
    {
        path: '/order',
        name: 'Order',
        meta: {
            keepAlive: true,  //此组件需要缓存  
        },
        component: () =>
            import('@/views/Order.vue')
    },

    {
        path: '/payment',
        name: "Payment",
        component: () =>
            import('@/views/Payment.vue')
    },

  
];


const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
})

// 全局路由守卫
router.beforeEach((to, from, next)=>{
    let nextRoute = ['Payment', 'Cart', 'Address', 'Order','AddressIndex','add-address'];
    //是否登录了
    let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'));
    // 判断要进入的路由是否需要验证登录
    if (nextRoute.indexOf(to.name) >= 0) {
        if (!userInfo) {
            router.push('/login');
        }
    }
    next();
})

export default router